<?php
//global $task;
global $error;
echo "Running $task";

switch($task){
	case "accessMatrix":
		handleAccessMatrix();
		break;
	case "newUserForm":
		handleNewUserForm();
		break;
	case 'listUser':
		handleListUser();
		break;
	case "class";
		handleClass();
		break;
	case "subject":
		handleSubject();
		break;
	case "department":
		handleDepartment();
		break;
	case "faculty":
		handleFaculty();
		break;
	case "logout":
		handleLogout();
		break;
	case "login":
		handleLogin();
		break;
	case "home":
		showMainPage();
		break;
	case "default":
		showLoginForm();
		break;
	default:
		handleUnknownOperation();
		break;
}

function handleAccessMatrix(){
	include_once "view/view_accessmatrix.php";
}

function handleNewUserForm(){
	include_once "view/view_newuser.php";
}

function handleListUser(){
	$userdao = new UserDAO();
	$users = $userdao->getList("gid > 1 ORDER BY gid, name");
	include_once "view/view_listuser.php";
}

function handleClass(){
	if(empty($_REQUEST['id'])){
		error_log( "No id." );
		$id = 0;
	} else {
		$id = $_REQUEST['id'];
	}
	$clazzdao = new ClazzDAO();
	$clazz = $clazzdao->get($id);

	$noticedao = new NoticeDAO();
	$notices = $noticedao->getList("pageid=5 AND referenceid = $id ORDER BY id DESC");

	$feedbackdao = new FeedbackDAO();
	$feedbacks = $feedbackdao->getList("pageid=5 AND referenceid = $id ORDER BY id DESC");

	include_once "view/view_class.php";
}

function handleUnknownOperation(){
	include_once "view/view_unknown.php";
}

function handleSubject() {
	if(empty($_REQUEST['id'])){
		error_log( "No id." );
		$id = 0;
	} else {
		$id = $_REQUEST['id'];
	}
	$subjectdao = new SubjectDAO();
	$subject = $subjectdao->get($id);

	$noticedao = new NoticeDAO();
	$notices = $noticedao->getList("schoolid=" . $_SESSION['school']->getId() . 
				" AND pageid=4 AND referenceid = $id ORDER BY id DESC");

	$clazzdao = new clazzDAO();
	$clazzes = $clazzdao->getList("subjectid = $id ORDER BY id");

//	$_REQUEST['subject'] = $subject;
//	$_REQUEST['notices'] = $notices;
//	$_REQUEST['clazzess'] = $clazzes;
	include_once "view/view_subject.php";
	
}

function handleDepartment(){
	if(empty($_REQUEST['id'])){
		error_log( "No id." );
		$id = 0;
	} else {
		$id = $_REQUEST['id'];
	}
	$departmentdao = new DepartmentDAO();
	$department = $departmentdao->get($id);

	$noticedao = new NoticeDAO();
	$notices = $noticedao->getList("schoolid=" . $_SESSION['school']->getId() . 
				" AND pageid=3 AND referenceid = $id ORDER BY id DESC");

	$subjectdao = new SubjectDAO();
	$subjects = $subjectdao->getList("departmentid = $id ORDER BY name");

	$_SESSION['departmentid'] = $department->getId();
	$_REQUEST['department'] = $department;
	$_REQUEST['notices'] = $notices;
	$_REQUEST['subjects'] = $subjects;
	include_once "view/view_department.php";
	
}

function handleFaculty() {
	if(empty($_REQUEST['id'])){
		echo "No id.";
		$id = 0;
	} else {
		$id = $_REQUEST['id'];
	}
	$facultydao = new FacultyDAO();
	$faculty = $facultydao->get($id);

	$noticedao = new NoticeDAO();
	$notices = $noticedao->getList("schoolid=" . $_SESSION['school']->getId() . 
				" AND pageid=2 AND referenceid = $id ORDER BY id DESC");

	$departmentdao = new DepartmentDAO();
	$departments = $departmentdao->getList("facultyid = $id ORDER BY name");

	$_REQUEST['faculty'] = $faculty;
	$_REQUEST['notices'] = $notices;
	$_REQUEST['departments'] = $departments;
	include_once "view/view_faculty.php";
}

function handleLogout(){
	unset( $_SESSION['user'] );
	unset( $_SESSION['accessmatrix'] );
	showLoginForm();
}

function handleLogin(){
	global $error;
	$username = empty( $_POST['username'] ) ? "" : $_POST['username'];
	$password = empty($_POST['password']) ? "" : $_POST['password'];
	// search user
	$userdao = new UserDAO();
	$ulist = $userdao->getList("login='$username' AND password=MD5('$password')");
	// if user found
	if(sizeof($ulist)>0){
		$user = $ulist[0];
		$_SESSION['user'] = $user;
		// Load access matrix from DB
		$accessmatrixdao = new AccessMatrixDAO();
		$accessmatrix = $accessmatrixdao->getList("1=1 ORDER BY task");
		$matrixarray = array();
		foreach($accessmatrix as $matrixitem){
			$mtask = $matrixitem->getTask();
			$mname = $matrixitem->getName();
			$mlevel = $matrixitem->getMinimumlevel();
			$matrixarray["$mtask"][$mname] = $mlevel;
//			$temp_array = array("$mtask" => 
//							array("name" => "$mname", "minimumlevel" => "$mlevel") 
//							);
//			array_push($matrixarray, $temp_array);
		}
//		echo "<pre>";
//		print_r($matrixarray);
//		echo "</pre>";
		$_SESSION['accessmatrix'] = $matrixarray;
		$_REQUEST['task'] = "default";
		// show main page
		showMainPage();
	} else {
	// show login with error
		$error = "Invalid username or password.";
		showLoginForm();
	}
}
    
function showMainPage() {
	$currentsemester = new Semester();
	$semesterdao = new SemesterDAO();
	$list = $semesterdao->getList("schoolid=" . $_SESSION['school']->getId() . " order by id desc limit 1");
	if(sizeof($list)>0){
		$_REQUEST['currsemester'] = $list[0];
		$_SESSION['currsemester'] = $list[0];
	}
	
	$noticedao = new NoticeDAO();
	$notices = $noticedao->getList("schoolid=" . $_SESSION['school']->getId() . 
				" AND pageid=1 ORDER BY id DESC");
	$_REQUEST['notices'] = $notices;
	
	$facultydao = new FacultyDAO();
	$faculties = $facultydao->getList("schoolid=" . $_SESSION['school']->getId() . " order by name");
	$_REQUEST['faculties'] = $faculties;
		
	include_once "view/view_mainpage.php";
}
	
function showLoginForm() {
	if(empty($_SESSION['user'])){
		include_once "view/view_login.php";
	} else {
		showMainPage();
	}	
}

?>